package com.gameley.dmgtpf.kernel.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gameley.dmgtpf.common.core.aspect.MyDoneTime;
import com.gameley.dmgtpf.common.utils.Result;
import com.gameley.dmgtpf.kernel.entity.RegionEntity;
import com.gameley.dmgtpf.kernel.service.RegionService;

/**
 * @author libencheng
 * @email libencheng@gmail.com
 * @date 2021-07-28 15:20:07
 */
@RestController
@RequestMapping("kernel/region")
public class RegionController
{
    @Autowired(required = false)
    private RegionService regionService;
    
    @MyDoneTime(param = "RegionController.updatetags")
    @ResponseBody
    @RequestMapping("/updatetags")
    public String updatetags(HttpServletRequest request, HttpServletResponse response,
            @RequestParam(value = "file", required = false) MultipartFile file)
    {
        return regionService.file2tableSavaDB(file);
    }
    
    /**
     * 列表
     */
    @MyDoneTime(param = "RegionController.list")
    @RequestMapping("/list")
    @RequiresPermissions("dmpf:region:list")
    public Result list(HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String, Object> params)
    {
        QueryWrapper<RegionEntity> wrapper = new QueryWrapper<RegionEntity>();
        wrapper.allEq(params);
        
        List<RegionEntity> list = regionService.list(wrapper);
        
        return Result.ok().put("page", list);
    }
    
    /**
     * 信息
     */
    @MyDoneTime(param = "RegionController.info")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("dmpf:region:info")
    public Result info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id)
    {
        RegionEntity region = regionService.getById(id);
        
        return Result.ok().put("region", region);
    }
    
    /**
     * 保存
     */
    @MyDoneTime(param = "RegionController.save")
    @RequestMapping("/save")
    @RequiresPermissions("dmpf:region:save")
    public Result save(HttpServletRequest request, HttpServletResponse response, @RequestBody RegionEntity region)
    {
        regionService.save(region);
        
        return Result.ok();
    }
    
    /**
     * 修改
     */
    @MyDoneTime(param = "RegionController.update")
    @RequestMapping("/update")
    @RequiresPermissions("dmpf:region:update")
    public Result update(HttpServletRequest request, HttpServletResponse response, @RequestBody RegionEntity region)
    {
        regionService.updateById(region);
        
        return Result.ok();
    }
    
    /**
     * 删除
     */
    @MyDoneTime(param = "RegionController.delete")
    @RequestMapping("/delete")
    @RequiresPermissions("dmpf:region:delete")
    public Result delete(HttpServletRequest request, HttpServletResponse response, @RequestBody Integer[] ids)
    {
        regionService.removeByIds(Arrays.asList(ids));
        
        return Result.ok();
    }
    
}
